import {Fragment, h, resolveComponent, defineAsyncComponent} from 'vue';
import {DataInfo, ChildProps} from '../resolve/types.ts';

export const renderChildren = (list: ChildProps[], info: DataInfo, data: any): [] => {
  const children = [];
  for (let i = 0; i < list.length; i++) {
    children.push(renderItem(list[i], info, data));
  }
  return children;
};

export const renderItem = (item: ChildProps, info: DataInfo, data: any) => {
  if (item.render) {
    return item.render(item, data, info);
  }
  if (item.text) {
    return h(Fragment, {}, [item.text]);
  }
  if (item.tagName) {
    return h(item.tagName, {
      // key:item.key,
      ...item.props,
      ...data,
    }, renderChildren(item.children, info, data));
  }
  if (item.component) {
    return h(item.component, {
      // key:item.key,
      ...item.props,
      ...getExtendProps(item, info, data),
    }, renderSlot(item, info, data));
  }
  if (item.type) {
    return h(resolveComponent(item.type), {
      // key:item.key,
      ...item.props,
      ...getExtendProps(item, info, data),
    }, renderSlot(item, info, data));
  }
};

export const renderSlot = (item: ChildProps, info: DataInfo, data: any) => {
  const slotsData = {};
  if (item.children && item.children.length > 0) {
    const _slotsData = {};
    for (let i = 0; i < item.children.length; i++) {
      const child = item.children[i];
      if (child) {
        const _slotName = child.name || 'default';
        const _children = _slotsData[_slotName] || [];
        _slotsData[_slotName] = _children;
        if (_children.length === 0) {
          slotsData[_slotName] = () => {
            return renderChildren(_children, info, data);
          };
        }
        _children.push(child);
      }
    }
  }
  return slotsData;
};

export const getExtendProps = (item: ChildProps, info: DataInfo, data: any) => {
  const prop = item.prop;
  const valueKey = item.valueKey;
  const updateKey = item.updateKey;
  const extendKeys = item.extendKeys;
  const disabledKey = item.disabledKey;
  const loadingKey = item.loadingKey;
  const extendProps = {};
  if (prop && valueKey && updateKey) {
    setDataProps(extendProps, data, prop, valueKey, updateKey);
  }
  if (extendKeys && extendKeys.length > 0) {
    for (let i = 0; i < extendKeys.length; i++) {
      const extendInfo = extendKeys[i];
      if (extendInfo) {
        setDataProps(extendInfo, data, extendInfo.prop, extendInfo.name, extendInfo.update);
      }
    }
  }
  if (disabledKey) {
    extendProps[disabledKey] = info.disabled;
  }
  if (loadingKey) {
    extendProps[loadingKey] = info.loading;
  }
  return extendProps;
};

export const setDataProps = (extendProps: object, data: any, prop: string, valueKey: string, updateKey?: string) => {
  extendProps[valueKey] = data[prop];
  if (updateKey) {
    extendProps[updateKey] = (value) => {
      data[prop] = value;
      // console.log('FxRender ---> setValue prop=', prop);
    };
  }
};

// import * as NaiveUI from 'naive-ui';
//
// export const getNaiveComponent = (component: string): any => {
//   // return NaiveUI[component];
// };
//
// export const getDefineComponent = (component: string): any => {
//   return resolveComponent(component);
// };
//
// export const getNaiveAsyncComponent = (component: string): any => {
//   return defineAsyncComponent(() =>
//     import('naive-ui').then(components => components[component])
//   );
// };
